package com.threeti.mecool.core.domain.model.plan.pojo;

import com.threeti.mecool.core.infrastructure.persistence.plan.TaskActionDataTypeRepository;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;

@Configurable
@Entity
@Table(name = "DM_WF_TASK_ACTION_DATA_TYPE",
        uniqueConstraints = @UniqueConstraint(columnNames = {"actionId", "name"}))
public class TaskActionDataType {
    private Long id;
    private Integer version;
    private String name;
    private int actionId;
    private TaskActionDataTypeRepository taskActionDataTypeRepository;

    private static TaskActionDataTypeRepository taskActionDataTypeRepository() {
        return new TaskActionDataType().taskActionDataTypeRepository;
    }

    @Transactional
    public void save() {
        taskActionDataTypeRepository.save(this);
    }

    @Transactional
    public void delete() {
        taskActionDataTypeRepository.delete(this);
    }

    public static TaskActionDataType findById(Long id) {
        return taskActionDataTypeRepository().findOne(id);
    }

    public static List<TaskActionDataType> findByActionId(int actionId) {
        return taskActionDataTypeRepository().findByActionId(actionId);
    }

    public static List<TaskActionDataType> findByActionIdAndName(int actionId, String name) {
        return taskActionDataTypeRepository().findByActionIdAndName(actionId, name);
    }

    public static TaskActionDataType findFirstByActionIdAndName(int actionId, String name) {
        List<TaskActionDataType> list = findByActionIdAndName(actionId, name);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public TaskActionDataType() {
    }

    public TaskActionDataType(int actionId, String name) {
        this();
        this.actionId = actionId;
        this.name = name;
    }

    //<editor-fold desc="getters/setters">

    @Transient
    @JsonIgnore
    public TaskActionDataTypeRepository getTaskActionDataTypeRepository() {
        return taskActionDataTypeRepository;
    }

    @Resource
    public void setTaskActionDataTypeRepository(TaskActionDataTypeRepository taskActionDataTypeRepository) {
        this.taskActionDataTypeRepository = taskActionDataTypeRepository;
    }

    public int getActionId() {
        return actionId;
    }

    public void setActionId(int actionId) {
        this.actionId = actionId;
    }

    @NotNull
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
    //</editor-fold>
}
